//package com.yaj.myfactorysupport.Interceptor;
//
//import cn.hutool.core.util.StrUtil;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.yaj.myfactorysupport.util.ErrorCodeEnum;
//import com.yaj.myfactorysupport.util.JWTUtils;
//import com.yaj.myfactorysupport.util.JsonData;
//import io.jsonwebtoken.Claims;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.PrintWriter;
//
///**
// * 登录检查
// */
//public class LoginInterceptor implements HandlerInterceptor {
//    /**
//     * 目标方法执行之前
//     * @param request
//     * @param response
//     * @param handler
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        try {
//            String accessToken = request.getHeader("token");
//            if (accessToken == null) {
//                accessToken = request.getParameter("token");
//            }
//
//            if (StrUtil.isNotBlank(accessToken)) {
//                Claims claims = JWTUtils.checkJWT(accessToken);
//                if (claims == null) {
//                    //告诉登录过期，重新登录
//                    sendJsonMessage(response, JsonData.buildError(ErrorCodeEnum.登录过期重新登录.getCode(),ErrorCodeEnum.登录过期重新登录.getMsg()));
//                    return false;
//                }
//
//                Integer id = (Integer) claims.get("id");
//                String name = (String) claims.get("name");
//
//                request.setAttribute("user_id", id);
//                request.setAttribute("name", name);
//
//                return true;
//            }
//        }catch (Exception e){
//
//        }
//        sendJsonMessage(response, JsonData.buildError(ErrorCodeEnum.登录过期重新登录.getCode(),ErrorCodeEnum.登录过期重新登录.getMsg()));
//        return false;
//    }
//
//    /**
//     * 目标方法执行完成以后
//     * @param request
//     * @param response
//     * @param handler
//     * @param modelAndView
//     * @throws Exception
//     */
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    /**
//     *
//     * @param request
//     * @param response
//     * @param handler
//     * @param ex
//     * @throws Exception
//     */
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//    }
//
//    /**
//     * 响应json数据给前端
//     * @param response
//     * @param obj
//     */
//    public static void sendJsonMessage(HttpServletResponse response, Object obj){
//
//        try{
//            ObjectMapper objectMapper = new ObjectMapper();
//            response.setContentType("application/json; charset=utf-8");
//            PrintWriter writer = response.getWriter();
//            writer.print(objectMapper.writeValueAsString(obj));
//            writer.close();
//            response.flushBuffer();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
